package org.railage.client.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.railage.client.beans.AuthBean;

/**
 * Servlet Filter implementation class ClientAuthFilter
 */
@WebFilter("/auth.xhtml")
public class AuthFilter implements Filter {
	
	public static final String MAIN_PAGE = "/railage-web/";

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest httpReq = (HttpServletRequest) req;
		HttpServletResponse httpResp = (HttpServletResponse) resp;
		
		if (httpReq.getSession().getAttribute(AuthBean.SESSION_USER_PARAM) == null) {
			chain.doFilter(req, resp);
		} else {
			httpResp.sendRedirect(MAIN_PAGE);
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		
	}

}
